<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Product_Widgets_ProductShowcase_Helper extends Zend_View_Helper_Abstract {

    
    /**
     * Get products for showcase
     * @author TrongHM
     * @since 1.0
     * @param mixed $category_ids
     * @param int $categoryLimit
     * @param int $productLimit
     * @param array $categoryOrder
     * @param array $productOrder
     * @param bool $isCategoryFeatured
     * @param bool $isProductFeatured
     * @return null 
     */
    public function getCategories($category_ids, $categoryLimit = 5, $productLimit = 10, $categoryOrder = array('id' => 'asc'), $productOrder = array('id' => 'asc'), $isCategoryFeatured = false, $isProductFeatured = false) {
        try {
            $model = Rabbit_Model_Factory::getInstance()->setModule('category')->getCategoryModel();
            $query = $model->getAdapter()->select();
            $query
                    ->from(array('c' => $model->getName()))
                    ->where('c.language = ?', $model->getLang())
                    ->limit($categoryLimit);

            if (is_array($category_ids) && !empty($category_ids)) {
                $query->where('c.id IN (?)', implode(',', $category_ids));
            } else if (is_numeric($category_ids)) {
                $query->where('c.id = ?', $category_ids);
            } else {
                $query->where('c.parent_id = 0')->where('id <> 1');
            }

            if ($isCategoryFeatured)
                $query->where('c.featured = 1');
            if ($categoryOrder) {
                foreach ($categoryOrder as $field => $type) {
                    $query->order('c.' . $field . ' ' . $type);
                }
            }
            $categories = $query->query()->fetchAll();
            if ($categories) {
                $categories = new Tomato_Model_RecordSet($categories, $model);
                $arrCategories = array();
                for($i = 0 ; $i < count($categories) ; $i++) {
                    $obj = new stdClass();
                    $obj->category = $categories[$i];
                    $obj->subCategories = $model->getSubCategories($categories[$i]->id);
                    $productModel = Rabbit_Model_Factory::getInstance()->setModule('product')->getProductModel();
                    $query = $productModel->getAdapter()
                            ->select('p.*')
                            ->from(array('p' => $productModel->getName()))
                            ->joinLeft(array('c' => $productModel->getPrefix() . 'category'), 'p.category_id = c.id', array('category_name' =>'c.name' , 'category_slug' => 'c.slug') )
                            ->where('p.category_id = ? ', $categories[$i]->id)
                            ->where('p.language = ?', $productModel->getLang());

                    if (isset($productLimit)) {
                        $query->limit($productLimit);
                    }
                    if ($productOrder) {
                        foreach ($productOrder as $field => $type) {
                            $query->order('p.' . $field . ' ' . $type);
                        }
                    }
                    if ($isProductFeatured) {
                        $query->where('p.featured = 1');
                    }
                    
                    $products = $query->query()->fetchAll();
                   
                    if ($products)
                        $products = new Tomato_Model_RecordSet($products, $productModel);
                     
                    
                    $obj->products = $products ;
                    
                    $arrCategories[] = $obj;
                }
                
                return $arrCategories;
            }
            else
                return null;
        } catch (Exception $ex) {
            echo $ex->getMessage();
        }
    }

}